package com.ruoyi.report_lqn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lqn.domain.ReportLqnRhlqcjlnfxjlbBaDetail;
import com.ruoyi.report_lqn.domain.vo.ReportLqnRhlqcjlnfxjlbBaDetailVo;
import com.ruoyi.report_lqn.mapper.ReportLqnRhlqcjlnfxjlbBaDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lqn.domain.bo.ReportLqnRhlqcjlnfxjlbBsInfoBo;
import com.ruoyi.report_lqn.domain.vo.ReportLqnRhlqcjlnfxjlbBsInfoVo;
import com.ruoyi.report_lqn.domain.ReportLqnRhlqcjlnfxjlbBsInfo;
import com.ruoyi.report_lqn.mapper.ReportLqnRhlqcjlnfxjlbBsInfoMapper;
import com.ruoyi.report_lqn.service.IReportLqnRhlqcjlnfxjlbBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 乳化沥青与粗集料的黏附性试验检测记录Service业务层处理
 *
 * @author tll
 * @date 2024-09-12
 */
@RequiredArgsConstructor
@Service
public class ReportLqnRhlqcjlnfxjlbBsInfoServiceImpl implements IReportLqnRhlqcjlnfxjlbBsInfoService {

    private final ReportLqnRhlqcjlnfxjlbBsInfoMapper baseMapper;

    private final ReportLqnRhlqcjlnfxjlbBaDetailMapper baDetailMapper;

    /**
     * 查询乳化沥青与粗集料的黏附性试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportLqnRhlqcjlnfxjlbBsInfoVo queryById(Long excelId){
        // 主表
        ReportLqnRhlqcjlnfxjlbBsInfoVo vo = baseMapper.selectVoById(excelId);
        // 子表
        LambdaQueryWrapper<ReportLqnRhlqcjlnfxjlbBaDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(excelId != null, ReportLqnRhlqcjlnfxjlbBaDetail::getExcelId, excelId);
        lqw.orderByAsc(ReportLqnRhlqcjlnfxjlbBaDetail::getCreateTime);
        List<ReportLqnRhlqcjlnfxjlbBaDetailVo> detailList = baDetailMapper.selectVoList(lqw);
        if (CollUtil.isNotEmpty(detailList)) {
            vo.setDetailList(detailList);
        }
        return vo;
    }

    /**
     * 查询乳化沥青与粗集料的黏附性试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportLqnRhlqcjlnfxjlbBsInfoVo> queryPageList(ReportLqnRhlqcjlnfxjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLqnRhlqcjlnfxjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLqnRhlqcjlnfxjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询乳化沥青与粗集料的黏附性试验检测记录列表
     */
    @Override
    public List<ReportLqnRhlqcjlnfxjlbBsInfoVo> queryList(ReportLqnRhlqcjlnfxjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportLqnRhlqcjlnfxjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLqnRhlqcjlnfxjlbBsInfo> buildQueryWrapper(ReportLqnRhlqcjlnfxjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLqnRhlqcjlnfxjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLqnRhlqcjlnfxjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLqnRhlqcjlnfxjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportLqnRhlqcjlnfxjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLqnRhlqcjlnfxjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLqnRhlqcjlnfxjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLqnRhlqcjlnfxjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLqnRhlqcjlnfxjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLqnRhlqcjlnfxjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLqnRhlqcjlnfxjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getYangpsj() != null, ReportLqnRhlqcjlnfxjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportLqnRhlqcjlnfxjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLqnRhlqcjlnfxjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLqnRhlqcjlnfxjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLqnRhlqcjlnfxjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLqnRhlqcjlnfxjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLqnRhlqcjlnfxjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLqnRhlqcjlnfxjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobmc()), ReportLqnRhlqcjlnfxjlbBsInfo::getBaobmc, bo.getBaobmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaobbh()), ReportLqnRhlqcjlnfxjlbBsInfo::getBaobbh, bo.getBaobbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLqnRhlqcjlnfxjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancr()), ReportLqnRhlqcjlnfxjlbBsInfo::getJiancr, bo.getJiancr());
        lqw.eq(StringUtils.isNotBlank(bo.getJilr()), ReportLqnRhlqcjlnfxjlbBsInfo::getJilr, bo.getJilr());
        lqw.eq(StringUtils.isNotBlank(bo.getFuhr()), ReportLqnRhlqcjlnfxjlbBsInfo::getFuhr, bo.getFuhr());
        lqw.eq(bo.getRiq() != null, ReportLqnRhlqcjlnfxjlbBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLqnRhlqcjlnfxjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLqnRhlqcjlnfxjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLqnRhlqcjlnfxjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getQuydd()), ReportLqnRhlqcjlnfxjlbBsInfo::getQuydd, bo.getQuydd());
        lqw.eq(StringUtils.isNotBlank(bo.getRuhlqpzdh()), ReportLqnRhlqcjlnfxjlbBsInfo::getRuhlqpzdh, bo.getRuhlqpzdh());
        lqw.eq(StringUtils.isNotBlank(bo.getRuhlqcd()), ReportLqnRhlqcjlnfxjlbBsInfo::getRuhlqcd, bo.getRuhlqcd());
        lqw.eq(StringUtils.isNotBlank(bo.getRuhlqzlbh()), ReportLqnRhlqcjlnfxjlbBsInfo::getRuhlqzlbh, bo.getRuhlqzlbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJilcdzl()), ReportLqnRhlqcjlnfxjlbBsInfo::getJilcdzl, bo.getJilcdzl());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyff()), ReportLqnRhlqcjlnfxjlbBsInfo::getShiyff, bo.getShiyff());
        return lqw;
    }

    /**
     * 新增乳化沥青与粗集料的黏附性试验检测记录
     */
    @Override
    public Boolean insertByBo(ReportLqnRhlqcjlnfxjlbBsInfoBo bo) {
        ReportLqnRhlqcjlnfxjlbBsInfo add = BeanUtil.toBean(bo, ReportLqnRhlqcjlnfxjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改乳化沥青与粗集料的黏附性试验检测记录
     */
    @Override
    public Boolean updateByBo(ReportLqnRhlqcjlnfxjlbBsInfoBo bo) {
        // 主表
        ReportLqnRhlqcjlnfxjlbBsInfo update = BeanUtil.toBean(bo, ReportLqnRhlqcjlnfxjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean upd = baseMapper.updateById(update) > 0;
        // 子表
        boolean upd2 = false;
        boolean insert = false;
        if (ObjectUtils.isNotEmpty(bo.getDetailList())) {
            // 根据excelId查询明细
            QueryWrapper<ReportLqnRhlqcjlnfxjlbBaDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("excel_id", bo.getExcelId());
            List<ReportLqnRhlqcjlnfxjlbBaDetail> list = baDetailMapper.selectList(queryWrapper);
            for (ReportLqnRhlqcjlnfxjlbBaDetail det : bo.getDetailList()) {
                if (ObjectUtils.isEmpty(list)) {
                    // 新增
                    insert = baDetailMapper.insert(det) > 0;
                } else if (ObjectUtils.isNotEmpty(det.getDetailId())) {
                    // 修改
                    upd2 = baDetailMapper.updateById(det) > 0;
                } else {
                    throw new ServiceException("更新主键不能为空, 请刷新后重试!");
                }
            }
        }
        return upd && upd2 && insert;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLqnRhlqcjlnfxjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除乳化沥青与粗集料的黏附性试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
